import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/home',
    component: HomeView
  },
  {
    path: '/comics',
    component: () => import('../views/Comics'),
    // children: [
    //   {
    //     path: ''
    //   }
    // ]
  },
  {
    path: '/deltails',
    component: () => import('../views/Details.vue')
  },
  {
    path: '/info',
    component: () => import('../components/NovelInfo')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from) => {
  // console.log('即将进入', to)
  // console.log('即将离开', from)
  if (to.path !== '/login') {
    const userToken = window.localStorage.getItem('userToken')
    if (!userToken) {
      return {
        path: '/login'
      }
    }
  }
})

export default router
